package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionBeans.GestionContactGroupLocal;
import sessionBeans.GestionContactLocal;
import entityBeans.Contact;
import entityBeans.ContactGroup;
import entityBeans.Enterprise;
import entityBeans.PhoneNumber;

/**
 * Servlet implementation class EditContactServlet
 */
public class EditContactServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Contact theContact;
	List<ContactGroup> groups;
	@EJB(name = "ContactBeanEntity", beanInterface = GestionContactLocal.class)
	GestionContactLocal daoContact;

	@EJB(name = "ContactGroupBeanEntity", beanInterface = GestionContactGroupLocal.class)
	GestionContactGroupLocal daoGroup;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditContactServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			System.out.println(request.getContextPath() + " "
					+ request.getServletPath() + " " + request.getPathInfo());

			int id = Integer.parseInt(request.getParameter("id"));
			Contact c = daoContact.searchById(id);
			if (c != null)
				theContact = c;
			else
				theContact = daoContact.searchEntById(id);

			List<ContactGroup> listGroup = daoGroup.getAllGroup();
			List<ContactGroup> cgs = daoGroup.getGroupsByContactId(id);
			if (cgs != null)
				groups = cgs;
			else
				groups = daoGroup.getGroupsByEntId(id);
			if (c != null) {
				request.setAttribute("contact", c);
				request.setAttribute("listGroup", listGroup);
				request.setAttribute("groups", groups);
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("pages/updateContact.jsp");
				dispatcher.forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String firstName = request.getParameter("firstname");
		String lastName = request.getParameter("lastname");
		String email = request.getParameter("email");
		String street = request.getParameter("street");
		String city = request.getParameter("city");
		String zip = request.getParameter("zip");
		// String country = request.getParameter("country");
		String phone1 = request.getParameter("phone1");
		String phone2 = request.getParameter("phone2");
		String phone3 = request.getParameter("phone3");
		String phonetype1 = request.getParameter("phonetype1");
		String phonetype2 = request.getParameter("phonetype2");
		String phonetype3 = request.getParameter("phonetype3");

		String gOther = request.getParameter("other");

		theContact.setFirstName(firstName);
		theContact.setLastName(lastName);
		theContact.setEmail(email);
		theContact.getAddress().setStreet(street);
		theContact.getAddress().setCity(city);
		theContact.getAddress().setZip(zip);

		// update phones
		((PhoneNumber) theContact.getPhones().toArray()[0])
				.setPhoneNumber(phone1);
		((PhoneNumber) theContact.getPhones().toArray()[0])
				.setPhoneKind(phonetype1);

		((PhoneNumber) theContact.getPhones().toArray()[1])
				.setPhoneNumber(phone2);
		((PhoneNumber) theContact.getPhones().toArray()[1])
				.setPhoneKind(phonetype2);

		((PhoneNumber) theContact.getPhones().toArray()[2])
				.setPhoneNumber(phone3);
		((PhoneNumber) theContact.getPhones().toArray()[2])
				.setPhoneKind(phonetype3);

		// update siret
		if (theContact instanceof Enterprise) {
			String siret = request.getParameter("siret");
			((Enterprise) theContact).setNumSiret(Integer.parseInt(siret));
		}

		List<String> cgList = new ArrayList<String>();
		for (ContactGroup cg : groups) {
			cgList.add(cg.getGroupName());
		}
		// update group of a contact
		String[] choix = request.getParameterValues("groups");
		List<String> newGrp = new ArrayList<String>();
		if (choix != null)
			for (String str : choix) {
				if (!str.equals(""))
					newGrp.add(str);
			}
		// create contact group
		if (gOther != null && !gOther.isEmpty()) {
			ContactGroup grp = new ContactGroup();
			grp.setGroupName(gOther);
			daoGroup.addGroup(grp);
			newGrp.add(gOther);
		}

		daoContact.updateContact(theContact, newGrp);

		try {

			if (!(theContact instanceof Enterprise)) {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("SearchContacts");
				dispatcher.forward(request, response);
			} else {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("SearchEnterprises");
				dispatcher.forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
